/*
This file is part of SmartShopper

SmartShopper is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

SmartShopper is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with vlc-remote.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <QString>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlField>
#include <QDebug>
#include "shoplistmodel.h"
#include "shoplistobject.h"
#include "shoplistcontrol.h"

ShopListControl::ShopListControl(ShopListModel * cModel, QObject *parent) :
    model(cModel), QObject(parent)
{
    mNameFilter = "";
//    sortModel << tr("by Name") << tr("by Shop") << tr("by State");
    mSortIndex = 0;
//    mSortOrder = byName;
    mSortDown = true;
    getListDb();
}

ShopListControl::~ShopListControl()
{
    model = NULL;
}

// ShopList controller with accessible from QML properies
// has setters for filter of objects
// can connect to DB, select, update

//TODO: functions
// - load list
//  - use filters
// - add object
// - remove object
// - update object
//

QString ShopListControl::nameFilter()
{
    return mNameFilter;
}

void ShopListControl::setNameFilter(const QString &cNameFilter)
{
    if (cNameFilter.compare(mNameFilter))
    {
        mNameFilter = cNameFilter;
        emit filterChanged();
    }
}

//int ShopListControl::storeFilter()
//{
//    return mStoreFilter;
//}

//void ShopListControl::setStoreFilter(const int cStoreFilter)
//{
//    if (cStoreFilter != mStoreFilter)
//        mStoreFilter = cStoreFilter;
//    emit storeFilterChanged();
//}

//int ShopListControl::stateFilter()
//{
//    return mStateFilter;
//}

//void ShopListControl::setStateFilter(const int cStateFilter)
//{
//    if (cStateFilter != mStateFilter)
//        mStateFilter = cStateFilter;
//    emit stateFilterChanged();
//}

bool ShopListControl::sortDown()
{
    return mSortDown;
}

void ShopListControl::setSortDown(const bool &cSortDown)
{
    if (cSortDown != mSortDown)
    {
        qDebug() << "Sort Down changed";
        mSortDown = cSortDown;
        getListDb();
        emit sortDownChanged();
    }
}

//ShopListControl::SortOrder ShopListControl::sortOrder()
//QString ShopListControl::sortOrder()
int ShopListControl::sortOrder()
{
    return mSortIndex;// mSortOrder;
}

//void ShopListControl::setSortOrder(const ShopListControl::SortOrder &cSortOrder)
//void ShopListControl::setSortOrder(const QString &cSortOrder)
void ShopListControl::setSortOrder(const int &cSortOrder)
{
    if (cSortOrder != mSortIndex)
    {
       qDebug() << "Sort Order changed";
        mSortIndex = cSortOrder;
        getListDb();
        emit sortOrderChanged();
    }

//    for (int i = 0; i < mSortStrings.count(); i++)
//    {
//        if (!cSortOrder.compare(mSortStrings[i]))
//        {
//            mSortIndex = i;
//            getListDb();
//            break;
//        }
//    }

}

//QList <QObject*> ShopListControl::getLists()
//{
//    return list;
//}

//QStringList ShopListControl::getSortList()
//{
//    return sortModel;
//}

void ShopListControl::getListDb()
{
    model->clear();

    QSqlQuery sql;
    QString sqlString;
    QString sqlSort(" order by ");
    sqlString = QString("select t.*, m.name as mname from  shoplist t "
                        "left join magazin m on t.magazinid = m.id");
    if (!mNameFilter.isEmpty())
        sqlString = sqlString + QString(" where name like '\%%1\%'").arg(mNameFilter);

    switch (mSortIndex) {
    case 0: sqlSort += QString(" t.name ");
        break;
//    case byNameDesc: sqlSort += QString(" t.name desc");
//        break;
    case 1: sqlSort += QString(" m.name");
        break;
//    case byStoreDesc: sqlSort += QString(" m.name desc");
//        break;
    case 2: sqlSort += QString(" t.state ");
        break;
//    case byStateDesc: sqlSort += QString(" t.state desc, t.name");
    }
    if (!mSortDown)
        sqlSort += QString(" desc");

    sqlString += sqlSort;
//    qDebug() << sqlString;
    sql.exec(sqlString);
    while (sql.next()) {
        QSqlRecord record = sql.record();
        ShopListObject obj;// = new ShopListObject();
        QString str = record.field("name").value().toString();
        obj.setName(str);
//        qDebug() << str;
        int shopId = record.field("magazinid").value().toInt();
        obj.setStoreId(shopId);
        obj.setStateId(record.field("state").value().toInt());
        obj.setId(record.field("state").value().toInt());
        // get Shop name
        str = record.field("mname").value().toString();
        if (str.isEmpty())
            obj.setShopName(tr("(unknown)"));
        else
            obj.setShopName(str);
        model->addShopList(obj);
//        list.append(obj);
    }
}

void ShopListControl::deleteItem(int cIndex)
{
    // remove item from DB
    ShopListObject &obj = model->getShopList(cIndex);
    int objId = obj.id();
    QSqlQuery sql;
    //TODO first - delete list items
    QString sqlString = QString("delete from shoplist where id = %1").arg(QString::number(objId));
    sql.exec(sqlString);
    model->removeShopList(cIndex);
}
